package practice;

import java.math.BigDecimal;
import java.math.BigInteger;

public class NumberTools <T extends Number & Comparable<T>>{
    private T a;
    private T b;

    public NumberTools(T a, T b) {
        super();
        this.a = a;
        this.b = b;
    }

    public T getNum(){
        if (a instanceof BigInteger){
            return (T) ((BigInteger)a).add((BigInteger) b);
        }else if (a instanceof BigDecimal){
            return (T) ((BigDecimal)a).add((BigDecimal) b);
        }else if (a instanceof Byte){
            return (T) (Byte.valueOf((byte)((Byte)a+(Byte)b)));
        }else if (a instanceof Short){
            return (T) (Short.valueOf((short)((Short)a+(Short) b)));
        }else if (a instanceof Integer){
            return (T) (Integer.valueOf((Integer)a+(Integer) b));
        }else if (a instanceof Long){
            return (T) (Long.valueOf((Long) a+(Long) b));
        }else if (a instanceof Float){
            return (T) (Float.valueOf((Float) a+(Float) b));
        }else if (a instanceof Double){
            return (T) (Double.valueOf((Double) a+(Double) b));
        }
        throw new UnsupportedOperationException("不支持该操作");
    }

    public T getSubtract(){
        if (a instanceof BigInteger){
            return (T) ((BigInteger)a).subtract((BigInteger) b);
        }else if(a instanceof BigDecimal){
            return (T) ((BigDecimal) a).subtract((BigDecimal)b);
        }else if(a instanceof Byte){
            return (T)(Byte.valueOf((byte)((Byte)a-(Byte)b)));
        }else if(a instanceof Short){
            return (T)(Short.valueOf((short)((Short)a-(Short)b)));
        }else if(a instanceof Integer){
            return (T)(Integer.valueOf((Integer)a-(Integer)b));
        }else if(a instanceof Long){
            return (T)(Long.valueOf((Long)a-(Long)b));
        }else if(a instanceof Float){
            return (T)(Float.valueOf((Float)a-(Float)b));
        }else if(a instanceof Double){
            return (T)(Double.valueOf((Double)a-(Double)b));
        }
        throw new UnsupportedOperationException("不支持该操作");
    }
}